package org.zeng.demo;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import org.zeng.demo.recyclerview.RecyclerViewSampleActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private FrameLayout mContentFyt;
    private Map<String, Class<? extends Activity>> mClassMap = new HashMap<>();
    private List<String> mKeyList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
    }

    private void initView() {
        mContentFyt = (FrameLayout) findViewById(R.id.main_content_fyt);
        mRecyclerView = (RecyclerView) findViewById(R.id.main_recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(new SampleAdapter());
    }


    private class ItemClickListener implements View.OnClickListener {
        private String mKey;

        public ItemClickListener(String pKey) {
            this.mKey = pKey;
        }

        @Override
        public void onClick(View v) {
            Class cls = mClassMap.get(mKey);
            Intent it = new Intent(MainActivity.this, cls);
            startActivity(it);
        }
    }

    private void initData() {
        mClassMap.put("RecyclerView", RecyclerViewSampleActivity.class);

        if (mClassMap.size() > 0) {
            Set<String> keySet = mClassMap.keySet();
            Iterator<String> iterator =
                    keySet.iterator();
            while (iterator.hasNext()) {
                mKeyList.add(iterator.next());
            }
        }

        TestLog.d(" keySize :  " + mKeyList.size());
    }


    private class SampleAdapter extends RecyclerView.Adapter<SampleHolder> {

        @Override
        public SampleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(MainActivity.this).inflate(android.R.layout.simple_list_item_1, parent, false);
            SampleHolder sh = new SampleHolder(view);
            return sh;
        }

        @Override
        public void onBindViewHolder(SampleHolder holder, int position) {
            holder.tv.setText(mKeyList.get(position));
            holder.tv.setOnClickListener(new ItemClickListener(mKeyList.get(position)));
        }

        @Override
        public int getItemCount() {
            return mKeyList == null ? 0 : mKeyList.size();
        }
    }

    private class SampleHolder extends RecyclerView.ViewHolder {

        private TextView tv;

        public SampleHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView;
        }
    }
}
